/**
 * @class common.base.tabModal
 * @extends common.base.object
 * 弹层基类，支持tab切换
<pre>
 define(function (require) {
    var Base = require('common/base/tabModal');
    return Base.extend({
        name: 'richtext',
        tpl: require('text!./templates/richtext.html'),
        getTabs: function () {
            var _this = this;
            return [
                {
                    name: 'form',
                    title: '编辑器',
                    ready: function () {
                        var _this = this;
                        _this.showContent();
                    }
                }
            ];
        }
    });
});
</pre>
 */
define(function (require) {
    require('slimscroll');
    require('jqui_resizable');
    require('jqui_draggable');
    var Base = require('common/base/view'),
        Tpl = require('text!./templates/tabModal.html');
    var dlg_top = 50;

    return Base.extend({
        /**
         * 构造函数
         * @param  {Object} params 配置参数
         */
        initialize: function (params) {
            var _this = this;
            _this.data = {};
            Base.prototype.initialize.apply(this, [params]);

            _this.params = _.extend({
                index: window.Z_INDEX++,
                name: _this.name
            }, params);
            _this.params.tabs = _this.parseTabs();
            this.current_tab_name = _this.params.tabs[0].name;
            _this._render();
            _this.showTab(this.current_tab_name);
        },
        /**
         * 解析tab对象
         */
        parseTabs: function () {
            var _this = this,
                tabs = _this.getTabs(),
                header_list = [];
            _.each(tabs, function (val, key) {
                header_list.push({
                    name: val.name,
                    title: val.title
                });
                _this.data[val.name] = {};
            });
            _.each(tabs, function (val, key) {
                val.header_list = header_list;
                val.do_ready = false;
            });
            return tabs;
        },
        resize: function () {
            return;
            var _this = this,
                scrollTop = $(_this.el).scrollTop(),
                window_height = $(window).height(),
                dialog_height = _this.$().height(),
                header_height = _this.$('.modal-header').height(),
                footer_height = _this.$('.modal-footer').height();
            if (scrollTop - dlg_top > 0) {
                _this.$('.modal-header').css({
                    'position': 'absolute',
                    'top': (scrollTop - dlg_top - 2) + 'px',
                    'width': _this.$().width() + 'px',
                    'z-index': 1000
                });
                _this.$('.pos_header').css({
                    'position': 'absolute',
                    'top': (scrollTop + _.intval(_this.$('.pos_header').data('offset'))) + 'px'
                });
            } else {
                _this.$('.modal-header').css({
                    'position': 'static'
                });
            }
            if (dialog_height - window_height - scrollTop - header_height > 0) {
                _this.$('.modal-footer').css({
                    'position': 'absolute',
                    'top': (scrollTop + window_height - footer_height - 62) + 'px',
                    'width': _this.$().width() + 'px',
                    'z-index': 1000
                });
            } else {
                _this.$('.modal-footer').css({
                    'position': 'static'
                });
            }
        },
        _render: function (returnHtml) {
            var _this = this,
                html = _this.render(Tpl, _this.params);
            _this.el = '#' + _this.id;
            if (returnHtml)
                return _this.html;
            if ($('.' + _this.name + '_modal').size() == 0) {
                $('body').append(html);

                $('.modal-title a', _this.el).click(function () {
                    _this.showTab($(this).data('name'));
                });
                $(_this.el).scroll(function () {
                    _this.resize();
                });
                $(window).resize(function () {
                    _this.resize();
                });
                $('.modal-dialog', _this.el).bind('content_changed', function () {
                    _this.resize();
                });
                $('.modal-dialog', _this.el).draggable({
                    handle: '.modal-header,.modal-footer',
                    stop: function (event, ui) {
                        $('.modal-dialog:hidden',_this.el).css({
                            left:ui.offset.left+'px',
                            top:ui.offset.top+'px'
                        });
                        //console.log(_this.el);
                    }
                });
            }
            $('.modal-dialog', _this.el).css({
                left: ($(window).width() / 2 - _this.$().width() / 2) + "px",
                top: dlg_top + 'px'
            });
            $(_this.el).show();
        },
        /**
         * 显示tab
         * @param  {String} name tab name
         */
        showTab: function (name) {
            this.current_tab_name = name;
            this.$().show().siblings().hide();
            this.$('.modal-title a[data-name="' + name + '"]').addClass('active').siblings().removeClass('active');
            var tab = _.get('$.tabs[?(@.name=="' + name + '")]', this.params);
            //if (tab.do_ready != true) {
            tab.ready.apply(this, [
                {
                    name: name
                }
            ]);
            // _.update('$.tabs[?(@.name=="' + name + '")].do_ready', true, this.params);
            this.resize();
            // }
        },
        /**
         * 设置当前tab的.modal-body内容
         */
        showContent: function () {
            var _this = this;
            _this.$('.modal-body').html(_this.render(_this.tpl, _.extend({
                tab_name: _this.current_tab_name,
                tab_section: 'body'
            }, _this.params, {
                data: _this.data[_this.current_tab_name]
            }))).removeClass('loading');

            _this.$('.modal-footer').html(_this.render(_this.tpl, _.extend({
                tab_name: _this.current_tab_name,
                tab_section: 'footer'
            }, _this.params, {
                data: _this.data[_this.current_tab_name]
            })));
            $(':radio.uniform, :checkbox.uniform').uniform();
            $('.close,.btn-cancel', _this.el).click(function () {
                _this.close();
            });
        },
        set_data: function (data) {
            this.data[this.current_tab_name] = data;
        },
        /**
         * 获取当前tab页内的dom元素
         * @param  {String} selector selector
         * @return {Object} jquery对象
         */
        $: function (selector) {
            var _this = this;
            _this.el = '#' + _this.id;
            if (_.empty(selector))
                return $(_this.el + ' .modal-dialog.' + this.current_tab_name);

            var _selector = [],
                res = _.explode(',', selector);

            _.each(res, function (val, key) {
                _selector.push(_this.el + ' .modal-dialog.' + _this.current_tab_name + " " + val);
            });
            return $(_.implode(',', _selector));
        },
        /**
         * 关闭modal
         */
        close: function () {
            var _this = this;
            $(_this.el).animate({}, 20, function () {
                if (_this.params.cache) {
                    $(_this.el).hide();
                } else {
                    $(_this.el).remove();
                }
            });
        }
    });
});
